/**
 \* Created with IntelliJ IDEA.
 \* @author: walter_long
 \* @time: 2018/11/15 12:01
 \* Description: 问卷state
 \*/
import API from "../../api/api_questionnaire";
import util from "../../common/util";
import en from "../../../../questionnairesurvey-wx/lib/wuxui/timeago/locales/en";

const state = {
    pageParam: {
        name: "",
        limit: 10,
        page: 1,
        state: ""
    },
    pageData: {
        currentPage: 1,
        pageSize: 5,
        pageSizes: [5],
        total: 0,
        rows: []
    },
    currentQuestionnaire: {
        id: '',
        name: '新建试卷',
        tag: '试卷说明',
        dimensions: [],
        questions: []
    },
    /**
     * QuestionnaireSelect的临时数据存储
     */
    questionnaireSelect: {
        selectId: ""
    }
};

const getters = {
    pageData: state => {
        return state.pageData
    },
    pageParam: state => {
        return state.pageParam
    },
    currentQuestionnaire: state => {
        return state.currentQuestionnaire;
    },
    questionnaireSelect: state => {
        return state.questionnaireSelect;
    }
};

const actions = {
    /**
     * 分页列出问卷
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    listQuestionnairesByPageAct({state, commit}, params) {
        let param;
        if (!params) {
            param = state.pageParam
        } else {
            param = {
                limit: params.limit ? params.limit : state.pageParam.limit,
                page: params.page ? params.page : state.pageParam.page,
                name: params.name ? params.name : state.pageParam.name,
                state: params.state ? params.state : state.pageParam.state
            }
        }
        commit('setPageParam', param);
        return new Promise((resolve, reject) => {
            API.listQuestionnairesByPage(param).then(result => {
                if (!result) {
                    reject(result)
                }
                result.pageSizes = [5];
                commit('setPageData', result);
                resolve(result)
            }, error => {
                reject(error)
            }).catch(err => {
                reject(err)
            })
        })
    },
    /**
     * 刷新分页数据
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    resetPageAct({state, commit}, params) {
        let param = {
            name: "",
            limit: 10,
            page: 1,
            state: ""
        };
        if (params) {
            param = {
                limit: params.limit ? params.limit : param.limit,
                page: params.page ? params.page : param.page,
                name: params.name ? params.name : param.name,
                state: params.state ? params.state : param.state
            }
        }
        commit('setPageParam', param);
        actions.listQuestionnairesByPageAct({state, commit})
    },
    /**
     * 删除一个问卷
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    deleteQuestionnaireAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.deleteQuestionnaire(params).then(result => {
                resolve(result)
            }, error => {
                reject(error)
            }).catch(err => {
                reject(err)
            })
        })
    },
    /**
     * 发布一个问卷
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    issueQuestionnaireAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.issueQuestionnaire(params).then(result => {
                resolve(result)
            }, error => {
                reject(error)
            }).catch(err => {
                reject(err)
            })
        })
    },

    setCurrentQuestionnaire({state, commit}, questionnaire) {
        commit("setCurrentQuestionnaire", questionnaire);
    },

    newQuestionnaire({state, commit}, questionnaire) {
        // console.log(JSON.stringify(questionnaire));
        return new Promise((resolve, reject) => {
            API.newQuestionnaire(questionnaire).then(result => {
                //commit("setCurrentQuestionnaire", questionnaire);
                resolve(result);
            }).catch(error => {
                reject(error);
            })
        })
    },

    getQuestionnaireAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.getQuestionnaire(params).then(result => {
                commit("setCurrentQuestionnaire", result);
                resolve(result);
            }).catch(error => {
                reject(error);
            })
        })
    },

    copyQuestionnaireAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            // console.log(JSON.stringify(params));
            API.copyQuestionnaire(params).then(result => {
                resolve(result);
            }).catch(error => {
                reject(error);
            })
        })
    },

    setQuestionnaireSelectAct({state, commit}, params) {
        commit("setQuestionnaireSelect", params);
    },

    /**
     * 删除单选题
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<unknown>}
     */
    deleteSingleChoiceQuestionAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.deleteSingleChoiceQuestion(params).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },

    /**
     * 删除维度
     * @param state
     * @param commit
     * @param params
     */
    deleteDimensionAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.deleteDimensionality(params).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },

    /**
     * 删除填空题
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<unknown>}
     */
    deleteFillUpAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.deleteFillUp(params).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },

    /**
     * 删除单选题选项
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<unknown>}
     */
    deleteChoiceAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.deleteChoice(params).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    uploadQuestionnaireAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.uploadQuestionnaire(params).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    }
};

const mutations = {
    setPageData(state, entity) {
        state.pageData = entity
    },
    setPageParam(state, entity) {
        state.pageParam = entity
    },

    setCurrentQuestionnaire(state, entity) {
        state.currentQuestionnaire = entity;
    },
    setQuestionnaireSelect(state, entity) {
        state.questionnaireSelect = entity;
    }
};

export default {
    state,
    getters,
    actions,
    mutations,
    namespaced: true
}
